package org.cy.peoplehill.client;

import org.cy.peoplehill.shared.FieldVerifier;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoginPanel extends Composite{
	public interface ExitHandler {
		public void onLoginSuccess(User user);
	}
	private final ExitHandler loginHandler;
	private TextBox nameField;
	
	public LoginPanel (ExitHandler lgHandler){
		this.loginHandler = lgHandler;
		final VerticalPanel panel = new VerticalPanel();
		final Label userNameLabel = new Label("Your Name:");
		nameField = new TextBox();
		nameField.setText("");
		final Button sendButton = new Button("Send");
		final Label errorLabel = new Label("");

		sendButton.addStyleName("sendButton");
		initWidget(panel);
		panel.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
		panel.add(userNameLabel);
		panel.add(nameField);
		panel.add(sendButton);
		panel.add(errorLabel);

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();

		// Create the pop up dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Login Result");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendUserToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendUserToServer();
				}
			}

			/**
			 * Send the user to the server and wait for a response.
			 */
			private void sendUserToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String userName = nameField.getText();
				if (!FieldVerifier.isValidName(userName)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				}
		
				// Then, we send the input to the server.
				sendButton.setEnabled(false);
				User user = new User(userName);
				textToServerLabel.setText(userName);
				serverResponseLabel.setText("");
				LoginServiceAsync loginService = (LoginServiceAsync)GWT.create(LoginService.class); 
				AsyncCallback<User> loginBack = new AsyncCallback<User>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox.setText("Login - Failure");
						serverResponseLabel.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(caught.getMessage());
						dialogBox.center();
						closeButton.setFocus(true);
					}
					public void onSuccess(User user) {
						loginHandler.onLoginSuccess(user);
					}
				};
				loginService.Login(user, loginBack);
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);	
	}
	
	public void setFocus(boolean focus) {
		nameField.setFocus(focus);
	}	
}
